#!/usr/bin/python
# encoding: utf-8
'''
@File    :   code.py
@Time    :   2020/03/11 21:54:19
@Author  :   fangkaijin 
@Version :   1.0
@Contact :   fangkaijin@gmail.com
@License :   杭州移动互联
@Desc    :   展示日志界面
'''

# 导入库包

import sys, os
import requests
from pathlib import Path
import zipfile
import tkinter as tk
import time
import threading

class ShowLog(object):
    def __init__(self, zipUrl):
        self.zipUrl = zipUrl
        self.root = tk.Tk()
        self._downloadZip_()
        self._showLogUi_()

    def _downloadZip_(self):
        response = requests.get(self.zipUrl, stream=True)
        zipF = Path('./' + self.zipUrl.split('/')[-1])
        if zipF.is_file():
            pass
        else:
            with open('./' + self.zipUrl.split('/')[-1], 'wb') as f:
                for chunk in response.iter_content(chunk_size=1024):
                    if chunk:
                        f.write(chunk)


    def _readLogInfo_(self):
        zipFile = self.zipUrl.split('/')[-1]
        self.zipRead = ZipRead(zipFile)

        if self.zipRead is not None and self.zipRead.namelist() is not None:
            self.UnzipFile = self.zipRead.namelist()[0]
            self.zipRead.extract(self.zipRead.namelist()[0], './')
            #print(self.zipRead.namelist()[0])
            time.sleep(1)
            #读取文件
            #子线程
            t = threading.Thread(target=self._readInfoSub, name='readLog')
            t.daemon = True
            t.start()

    def _readInfoSub(self):
        with open(self.zipRead.namelist()[0], 'rb') as f:
            while True:
                line = f.readline()
                if line:
                    try:
                        self.tx.insert('insert', line.decode('utf8', 'ignore'))
                    except:
                        pass
                else:
                    break

    def _showLogUi_(self):
        # 设置窗口标题:
        self.root.title(self.zipUrl.split('/')[-1])
        self.root.geometry('900x600+300+100')

        vBar = tk.Scrollbar(self.root)
        vBar.pack(side=tk.RIGHT, fill=tk.Y)

        self.tx = tk.Text(self.root, width = 900,
                          height = 600, font=('Aaril', 16),
                          yscrollcommand=vBar.set,
                          fg='blue',
                          padx = 5,
                          pady = 5,
                          wrap = tk.WORD,
                          spacing2 = 5)
        vBar.config(command=self.tx.yview)
        self.tx.pack()
        self.root.protocol("WM_DELETE_WINDOW", self._windowClose_)
        self._readLogInfo_()
        # 消息循环显示
        self.root.mainloop()

    def _windowClose_(self):
        '''
        删除创建的文件
        :return:
        '''
        print('删除创建的文件')
        os.chdir('./')
        zipF = Path('./' + self.zipUrl.split('/')[-1])
        uzipF = Path('./' + self.UnzipFile)
        if zipF.is_file():
            os.remove(zipF)
            os.remove(uzipF)
        self.zipRead.close()
        self.root.destroy()




class ZipRead(zipfile.ZipFile):
    def __init__(self, zipString):
        zipfile.ZipFile.__init__(self, zipString)



if __name__ == '__main__':
    ShowLog('https://cdnvip.meishubao.com/logs/2020-04/01/658d26e4ff01c0a11586a45a6634175a.zip')